package com.zzh.configuration;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


//@Configuration 作用：让Spring扫描这个配置类，
@Configuration
public class ChatConfiguration {
    @Bean//必须加Bean注解，spring才会管理这个javaBean对象
    public ChatClient  chatClient(OllamaChatModel ollamaChatModel) {

        //创建一个日志通知（目的是为了增强chatClient的功能）
        SimpleLoggerAdvisor simpleLoggerAdvisor = SimpleLoggerAdvisor.builder().build();

        MessageChatMemoryAdvisor chatMemoryAdvisor = MessageChatMemoryAdvisor
                .builder(chatMemory()).build();


        return ChatClient.builder(ollamaChatModel)
                .defaultAdvisors(simpleLoggerAdvisor,chatMemoryAdvisor)
                .defaultSystem("你是宜宾学院的AI，你的名字叫YY.")
                .build();
    }

    @Bean
    public ChatMemory chatMemory(){
        //定义一个用于存储聊天记录的对象，采用了Spring AI提供的现场类（储存在内存中，Map<chatId, List<Message>>）
        return MessageWindowChatMemory.builder().build();
    }
}
